
//  Created by 莫瑞权 on 2022/5/2.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFCenteredleLabel: UIButton {
    @objc public let name = UILabel()
    @objc public var redDotLayer:CAShapeLayer?
    @objc public var fullScreen:NSInteger = 0 //不修改imageView

    override func layoutSubviews() {
        super.layoutSubviews()
        if (self.fullScreen==0) {
                self.name.frame = CGRect(x: self.frame.size.width * 0.1, y: self.frame.size.height * 0.1, width: self.frame.size.width - (self.frame.size.height * 0.2), height: self.frame.size.height - (self.frame.size.height * 0.2))
        
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
       
        self.name.textColor = UIColor.white
        self.addSubview(self.name)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //画小红点
    @objc public func drawRedDot()
    {
        if(self.redDotLayer==nil){
            
            self.clipsToBounds = false;
            
            
            self.redDotLayer = CAShapeLayer()
            if (self.fullScreen==2) {
                // 指定frame，
                self.redDotLayer!.frame = CGRect(x: self.frame.size.width-9.0*0.75-4.5, y: 0-9.0*0.25+4.5, width: 9, height: 9)
              
            }else{
                // 指定frame，
                self.redDotLayer!.frame = CGRect(x: self.frame.size.width - 9.0*0.75, y: 0 - 9.0*0.25, width: 9, height: 9)
            }
            self.redDotLayer!.masksToBounds = true
            
            self.redDotLayer!.cornerRadius = self.redDotLayer!.frame.width/2;
            
            // 设置填充颜色
            self.redDotLayer!.fillColor = UIColor.red.cgColor
            // 设置线宽
            self.redDotLayer!.lineWidth = 1
            // 设置线的颜色
            self.redDotLayer!.strokeColor = UIColor.red.cgColor
            // 使用UIBezierPath创建路径
            
            
        let circlePath = UIBezierPath.init(rect: CGRect(x: 0, y: 0, width: self.redDotLayer!.frame.size.width, height: self.redDotLayer!.frame.size.height))

            
            // 设置CAShapeLayer与UIBezierPath关联
            self.redDotLayer!.path = circlePath.cgPath;
            
            self.redDotLayer!.strokeStart = 0.0;
            self.redDotLayer!.strokeEnd =  1.0;
            // 将CAShaperLayer放到某个层上显示
            
            self.layer.addSublayer(self.redDotLayer!)
            
        }
    }
   
  
    //清除小红点
    @objc public func clearRedDot()
     {
        if ((self.redDotLayer) != nil) {
            self.redDotLayer!.removeFromSuperlayer()
            self.redDotLayer  = nil
        }
    }

}
